<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useResumeStore } from './stores/resumeStore';
import { UserOutlined } from '@ant-design/icons-vue';
import { useRouter } from 'vue-router';

const resumeStore = useResumeStore();
const router = useRouter();
const loading = ref(true);

onMounted(async () => {
  try {
    await resumeStore.initialize();
  } catch (error) {
    console.error('初始化应用失败:', error);
  } finally {
    loading.value = false;
  }
});

const createNewResume = async () => {
  try {
    const newResume = await resumeStore.createResume({
      name: '新建简历'
    });
    router.push(`/resume/edit/${newResume.id}`);
  } catch (error) {
    console.error('创建简历失败:', error);
  }
};

const goToResumeList = () => {
  router.push('/resume/list');
};
</script>

<template>
  <a-config-provider :theme="{ token: { colorPrimary: '#1890ff' } }">
    <div class="min-h-screen bg-gray-100 dark:bg-gray-900">
      <!-- 加载状态 -->
      <div v-if="loading" class="flex items-center justify-center min-h-screen">
        <a-spin size="large" tip="加载中..." />
      </div>
      
      <!-- 主应用 -->
      <template v-else>
        <!-- 顶部导航栏 -->
        <a-layout>
          <a-layout-header class="bg-white dark:bg-gray-800 shadow px-4 flex items-center justify-between">
            <div class="flex items-center">
              <h1 class="text-xl font-bold text-primary cursor-pointer" @click="() => router.push('/')">简历大师</h1>
            </div>
            <div class="flex items-center space-x-4">
              <a-button type="primary" @click="createNewResume">新建简历</a-button>
              <a-button @click="goToResumeList">我的简历</a-button>
              <a-dropdown>
                <a-button>
                  <template #icon><UserOutlined /></template>
                  我的账户
                </a-button>
                <template #overlay>
                  <a-menu>
                    <a-menu-item key="1">个人设置</a-menu-item>
                    <a-menu-item key="2">退出登录</a-menu-item>
                  </a-menu>
                </template>
              </a-dropdown>
            </div>
          </a-layout-header>
          
          <a-layout-content class="p-6">
            <div class="container mx-auto">
              <!-- 使用路由视图 -->
              <router-view></router-view>
            </div>
          </a-layout-content>
          
          <a-layout-footer class="text-center bg-white dark:bg-gray-800 py-4">
            简历大师 ©2023 Created by Resume Master Team
          </a-layout-footer>
        </a-layout>
      </template>
    </div>
  </a-config-provider>
</template>

<style>
#app {
  width: 100%;
  height: 100vh;
  margin: 0;
  padding: 0;
  text-align: left;
}

/* 暗色模式适配 */
.dark .ant-layout-header,
.dark .ant-layout-footer {
  background-color: #1f1f1f;
  color: rgba(255, 255, 255, 0.85);
}

.dark .ant-layout-content {
  background-color: #141414;
}
</style>
